package com.qixiucoding;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.JSONReader;
import com.jayway.jsonpath.JsonPath;
import lombok.SneakyThrows;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.jsoup.nodes.Element;
import org.junit.jupiter.api.Test;
import org.seimicrawler.xpath.JXDocument;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;

import java.io.File;

/**
 * @Description TODO
 * @Author longhj
 * @Date 2022/05/01 星期日 0:10
 */
@SpringBootTest
public class LearnXPath {
    // 标准的线路格式，对HTML的格式有些不适用
    @SneakyThrows
    @Test
    public void dom4jxpathLearn(){
        StringBuilder htmlSB = new StringBuilder();
        // HttpRequest request = HttpUtil.createGet("https://www.scfund.com.cn/");
        // HttpResponse execute = request.execute();
        // System.out.println(execute.body());
        ClassPathResource resource = new ClassPathResource("test.xml");
        File file = resource.getFile();
        SAXReader reader = new SAXReader();
        Document read = reader.read(file);
        Node node = read.selectSingleNode("//*[@id=\"gdlc\"]");
        System.out.println(node.getText());
    }
    @SneakyThrows
    @Test
    public void jsoupxpathLearn(){
        ClassPathResource resource = new ClassPathResource("test.xml");
        File file = resource.getFile();
        JXDocument document = JXDocument.create(FileUtil.readString(file, "utf-8"));
        Element one = (Element) document.selOne("//*[@id=\"gdlc\"]");
        System.out.println(one.text());
    }
    @SneakyThrows
    @Test
    public void jaywayJsonpathLearn(){
        ClassPathResource resource = new ClassPathResource("test.json");
        File file = resource.getFile();
        Object read = JsonPath.read(FileUtil.readString(file, "utf-8"), "$.data[?(@.CATEGORYNAME=='债券型'&&@.FUNDCODE=~/01306.*/i)].FUNDFULLNAME");
        System.out.println(read.toString());
    }

    /**
     * fastJson2学习jsonpath:目前发现需要把过滤条件分开写
     */
    @SneakyThrows
    @Test
    public void fastJson2JsonpathLearn(){
        ClassPathResource resource = new ClassPathResource("test.json");
        File file = resource.getFile();
        com.alibaba.fastjson2.JSONPath path = com.alibaba.fastjson2.JSONPath.of("$.data[?(@.CATEGORYNAME=='债券型')?(@.FUNDCODE=~/01306.*/i)].FUNDFULLNAME");
        com.alibaba.fastjson2.JSONReader parser = com.alibaba.fastjson2.JSONReader.of(FileUtil.readString(file, "utf-8"));
        Object read = path.extract(parser);
        System.out.println(read.toString());
        Object eval = JSONPath.extract(FileUtil.readString(file, "utf-8"), "$.data[?(@.CATEGORYNAME=='债券型')?(@.FUNDCODE=~/01306.*/i)].FUNDFULLNAME");
        System.out.println(eval);
    }
    @SneakyThrows
    @Test
    public void fastJsonJsonpathLearn(){
        ClassPathResource resource = new ClassPathResource("test.json");
        File file = resource.getFile();
        com.alibaba.fastjson.JSONPath path = com.alibaba.fastjson.JSONPath.compile("$.data[?(@.CATEGORYNAME=='债券型'&&@.FUNDCODE=~/01306.*/i)].FUNDFULLNAME");
        Object parser = path.eval(FileUtil.readString(file, "utf-8"));
        System.out.println(parser.toString());
        Object read = com.alibaba.fastjson.JSONPath.read(FileUtil.readString(file, "utf-8"), "$.data[?(@.CATEGORYNAME=='债券型'&&@.FUNDCODE=~/01306.*/i)].FUNDFULLNAME");
        System.out.println(read);
    }

}
